<template>
  <div class="forget-panel">
    <div class="header">
      <div style="width: 700px">
        <a-steps :current="current">
          <template #progressDot="{ index, status, prefixCls }">
            <a-popover>
              <!-- <template #content>
              <span>step {{ index }} status: {{ status }}</span>
            </template> -->
              <span :class="`${prefixCls}-icon-dot`" />
            </a-popover>
          </template>
          <a-step title="填写账号">
            <!-- <template #icon>
              <question-circle-outlined style="color: #0EC671" />
            </template> -->
          </a-step>
          <a-step title="重置密码">
            <!-- <template #icon>
              <issues-close-outlined style="color: #0EC671" />
            </template> -->
          </a-step>
          <a-step title="成功">
            <!-- <template #icon>
              <check-circle-outlined style="color: #0EC671" />
            </template> -->
          </a-step>
        </a-steps>
      </div>
    </div>
    <div class="body">
      <a-card v-if="current === 0" class="item">
        <div style="width: 400px">
          <a-form layout="horizontal" ref="step0" :model="tform" :rules="trules" :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
            <a-form-item label="手机号:" name="telephone">
              <a-input v-model:value="tform.telephone" placeholder="请输入手机号" allow-clear autocomplete="off" :maxlength="11" />
            </a-form-item>
            <a-form-item label="图形验证码:" name="validcode">
              <a-input v-model:value="tform.validcode" placeholder="请输入图形验证码" allow-clear autocomplete="off" :maxlength="5">
                <template #addonAfter>
                  <valid-code ref="refsValidCode" v-model:value="validCode" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="验证码:" name="verificationCode">
              <a-input-search v-model:value="tform.verificationCode" placeholder="请输入短信验证码" allow-clear autocomplete="off" :maxlength="8" enter-button="获取验证码" @search="onSearch">
              </a-input-search>
            </a-form-item>
          </a-form>
          <a-button type="primary" :block="true" @click="toStep(1)">下一步</a-button>
        </div>
      </a-card>
      <a-card v-if="current === 1" class="item">
        <div style="width: 400px">
          <a-form layout="horizontal" ref="step1" :model="form" :rules="rules" :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
            <a-form-item label="新密码:" name="password">
              <a-input-password v-model:value="form.password" type="password" placeholder="请输入新密码" autocomplete="off" :maxlength="50" />
            </a-form-item>
            <a-form-item label="确认新密码:" name="passwordT">
              <a-input-password v-model:value="form.passwordT" type="password" placeholder="请确认新密码" autocomplete="off" :maxlength="50" />
            </a-form-item>
          </a-form>
          <a-button type="primary" :block="true" @click="toStep(2)">下一步</a-button>
        </div>
      </a-card>
      <a-card v-if="current === 2" class="item">
        <a-result status="success">
          <template #title>
            <div class="title">新密码设置成功，<span @click="toLogin">立即登录</span></div>
          </template>
        </a-result>
      </a-card>
    </div>
  </div>
</template>

<script setup name="forget">
import { ref, reactive } from 'vue'
import { QuestionCircleOutlined, IssuesCloseOutlined, CheckCircleOutlined } from '@ant-design/icons-vue'
import ValidCode from '@/components/Bussiness/ValidCode.vue'
import { message } from 'ant-design-vue'
import router from '@/router'
import { resetPwd, sendModPwdSms } from '@/api/authorize'

const current = ref(0)
const step0 = ref(null)

const validCode = ref('')
const tform = reactive({})
let validateValidCode = async (_rule, value) => {
  if (!value) {
    return Promise.reject('请输入图形验证码')
  } else {
    if (validCode.value !== value) {
      return Promise.reject('请输入正确的图形验证码')
    }
    return Promise.resolve()
  }
}
const trules = reactive({
  telephone: [
    { required: true, message: '请输入手机号', type: 'string', trigger: 'blur' },
    { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确的手机号' },
  ],
  validcode: [{ required: true, validator: validateValidCode, trigger: 'blur' }],
  verificationCode: [{ required: true, message: '请输入短信验证码', type: 'string', trigger: 'blur' }],
})

const step1 = ref(null)
const form = reactive({})
const rules = reactive({
  password: [
    { required: true, message: '请输入新密码', type: 'string', trigger: 'blur' },
    { pattern: /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/, message: '密码复杂度不达标，需要包含大小写字母、特殊符号、数字的三种，且介于6-50位之间。' },
  ],
  passwordT: [{ required: true, message: '请确认新密码', type: 'string', trigger: 'blur' }],
})

const toLogin = () => router.push(`/user`)
const toStep = async (step) => {
  if (step === 1) {
    // 校验表单
    await step0.value.validate()
    current.value = step
  } else {
    // 校验表单
    await step1.value.validate()

    const params = { ...tform, ...form }
    delete params.validcode
    const res = await resetPwd(params)
    if (res.success) {
      current.value = step
    } else {
      message.error(res.msg)
    }
  }
}
const onSearch = async () => {
  await sendModPwdSms(tform.telephone)
}
</script>

<style lang="less" scoped>
@import url('@/assets/style/authorize.less');
.forget-panel {
  width: 100%;
  height: 100%;
  background: #f6f7fa;
  text-align: left;
  display: flex;
  flex-flow: column nowrap;
  // justify-content:flex-start;
  padding-top: 50px;
  align-items: center;
  .header {
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .body {
    display: flex;
    justify-content: center;
    align-items: center;
    .title {
      font-size: 14px;
      font-weight: bold;
      span {
        color: #e1251b;
        cursor: pointer;
      }
    }
    .item {
      width: 800px;
      height: 400px;
      padding: 20px 0;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
